package com.health.trans.processor.writer;

import com.health.trans.annotation.Writer;
import com.health.trans.kit.WriterConfig;
import com.health.trans.pojo.JobConfig;
import com.health.trans.util.DataSourceUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.database.JpaItemWriter;

import java.util.List;

/**
 * @author nqlz
 */
@Slf4j
@Writer
public class MySqlWriter implements CsWriter {


    @Override
    public <T> ItemWriter<T> getWriter(WriterConfig<T> writerConfig) {
        JpaItemWriter<T> tMySqlWriter = new JpaItemWriter<T>() {
            private final JobConfig<T> jobConfig = writerConfig.getJobConfig();
            @Override
            public void write(List<? extends T> list) {
                super.write(list);
                jobConfig.log(list.size());
            }
        };
        tMySqlWriter.setUsePersist(writerConfig.isUsePersist());
        tMySqlWriter.setEntityManagerFactory(DataSourceUtil.getEntityManagerFactory(writerConfig.getSourceName()));
        return tMySqlWriter;
    }


}
